package com.jy.api.service.impl;

import com.jy.api.dao.CashCouponTransactionDao;
import com.jy.api.domain.CashCouponStatus;
import com.jy.api.domain.CashCouponTransaction;
import com.jy.api.service.CashCouponTransactionService;
import com.jy.api.signUpReq.admin.MemberCashCouponRes;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by xiaohua on 2019/1/18.
 */
@Service
public class CashCouponTransactionServiceImpl implements CashCouponTransactionService {
    @Autowired
    private CashCouponTransactionDao transactionDao;

    @Override
    public Page<CashCouponTransaction> findAllByStoreId(String storeId, String type, Pageable pageable) {
        if (StringUtils.equals("SEND", type)) {
            return transactionDao.findByStoreIdAndStatusOrderByGiveTimeDesc(storeId, pageable);
        } else {
            return transactionDao.findByStoreIdAndStatusOrderByUseTimeDesc(storeId, CashCouponStatus.VERIFIED, pageable);
        }
    }

    @Override
    public MemberCashCouponRes findAllByMemberCash(String memberId, String storeId) {
        MemberCashCouponRes res = new MemberCashCouponRes();
        List<MemberCashCouponRes.CashService> cashServices = new ArrayList<>();
        BigInteger integer = transactionDao.findByUse(memberId, CashCouponStatus.VERIFIED, storeId);
        List<CashCouponTransaction> list = transactionDao.findByMemberIdAndStatusAndStoreIdOrderByGiveTimeDesc(memberId, CashCouponStatus.NORMAL, storeId);
        int count = 0;
        for (CashCouponTransaction transaction : list) {
            MemberCashCouponRes.CashService cashCouponRes = new MemberCashCouponRes.CashService();
            cashCouponRes.setEndTime(transaction.getEndTime());
            cashCouponRes.setStartTime(transaction.getStartTime());
            cashCouponRes.setName(transaction.getCouponName());
            cashServices.add(cashCouponRes);
            count += 1;
        }
        res.setCashServices(cashServices);
        res.setHave(count);
        res.setUse(Integer.valueOf(integer.toString()));
        return res;
    }

    @Override
    @Transactional
    public List<CashCouponTransaction> findAllByMemberIdAndStoreId(String memberId, String storeId) {
        List<CashCouponTransaction> transactions = transactionDao.findByMemberIdAndStatusAndStoreIdAndEndTimeLessThanEqualOrderByGiveTimeDesc(memberId, CashCouponStatus.NORMAL, storeId, new Date());
        List<CashCouponTransaction> newCash = new ArrayList<>();
        for (CashCouponTransaction transaction : transactions) {
            transaction.setStatus(CashCouponStatus.EXPIRED);
            newCash.add(transaction);
        }
        transactionDao.save(newCash);
        List<CashCouponTransaction> list = transactionDao.findByMemberIdAndStatusAndStoreIdAndEndTimeIsGreaterThanEqualOrderByGiveTimeDesc(memberId, CashCouponStatus.NORMAL, new Date(), storeId);
        return list;
    }
}
